#include "board.h"
#include <string.h>
#include <stdio.h>
#include "console.h"

#define LOG_TAG "Free"
#define LOG_LVL ELOG_LVL_INFO
#include "elog.h"

#include "board.h"
#include "FreeRTOS.h"
#include "task.h"
#include <string.h>
#include "timers.h"
#include "semphr.h"
#include <stdlib.h>

void timer_toggle_callback(TimerHandle_t timer_handler);

SemaphoreHandle_t timer_toggle_binary;
TimerHandle_t timer_handler;

void task1(void* pvParameters)
{
    while (1)
    {
        vTaskDelay(500);
    }
}

void timer_toggle_callback(TimerHandle_t timer_handler)
{
    xSemaphoreGive(timer_toggle_binary);
}

void init_task(void *pvParameters)
{
    taskENTER_CRITICAL();
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
    timer_toggle_binary = xSemaphoreCreateBinary();
    led_lowlever_init(led1);
    led_lowlever_init(led2);
    led_lowlever_init(led3);
    xTaskCreate(task1, "task1", 256, NULL, 4, NULL);
    timer_handler = xTimerCreate("led_toggle", pdMS_TO_TICKS(1 * 1000), 0, (void*)1, timer_toggle_callback);
    xTimerStart(timer_handler, pdMS_TO_TICKS(2 * 1000));
    taskEXIT_CRITICAL();
    
    vTaskDelete(NULL);
}

int main(void) 
{
    xTaskCreate(init_task, "init_task", 1024, NULL, 1, NULL);
    
    vTaskStartScheduler();
}

